package com.youlu.campus.web.ca.controller;
import com.youlu.campus.entity.PayConfigPage;
import com.youlu.campus.entity.VO.req.PointPlaceOrderVO;
import com.youlu.campus.entity.VO.req.UserCaQueryVO;
import com.youlu.campus.entity.ca.dto.CaAdPopusVo;
import com.youlu.campus.service.activity.impl.ActivityPayConfigService;
import com.youlu.campus.service.ca.CaReceiveAdminService;
import com.youlu.campus.service.ca.UserCaReceiveHistoryService;
import com.youlu.campus.service.ca.UserCaService;
import com.youlu.campus.service.ca.UserPaperCaReceiveHistoryService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.youlu.campus.entity.VO.req.UserCaReceivePointReq;
import com.youlu.campus.entity.english.vo.request.CompetitionCaCollectRequest;
import com.youlu.campus.service.visitor.UserPointsService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 用户证书控制器
 */
@RestController
public class UserCaController {

    @Autowired
    private UserPointsService userPointsService;

    @Autowired
    private UserCaService userCaService;

    @Autowired
    private UserCaReceiveHistoryService userCaReceiveHistoryService;

    @Autowired
    private CaReceiveAdminService caReceiveAdminService;

    @Autowired
    private ActivityPayConfigService activityPayConfigService;

    @Autowired
    private UserPaperCaReceiveHistoryService userPaperCaReceiveHistoryService;

    @PostMapping("/mini-app/user-ca/place-order")
    public QResult placeOrder(@RequestBody PointPlaceOrderVO req, HttpServletRequest httpRequest) {
        req.setClientIp(CommonUtils.getRemoteIp(httpRequest));
        req.setUserId(RequestContext.getUserId());
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        return QResult.success(userCaService.placeOrderByPoint(req, RequestContext.getCurUser()));
    }

    /**
     * 校验是否满足积分扣减
     *
     * @param userCaReceivePointReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/ca-receive/check-point")
    public QResult checkReceiveCaPoint(@RequestBody @Validated UserCaReceivePointReq userCaReceivePointReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        userCaReceivePointReq.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(userPointsService.checkReceiveCaPoint(userCaReceivePointReq));
    }

    /**
     * 领取电子证书
     *
     * @param caCollectReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/ca-receive/ca/do")
    public QResult caCollect(@RequestBody @Validated CompetitionCaCollectRequest caCollectReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        caCollectReq.setCurrentUser(RequestContext.getCurUser());
        caCollectReq.setQaReceive(Boolean.TRUE);
        return QResult.success(userCaService.caReceive(caCollectReq));
    }

    /**
     * 领取电子证书
     *
     * @param caCollectReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/plus/ca-receive/ca/do")
    public QResult caCollectPlus(@RequestBody @Validated CompetitionCaCollectRequest caCollectReq,
                              BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        caCollectReq.setCurrentUser(RequestContext.getCurUser());
        caCollectReq.setQaReceive(Boolean.TRUE);
        caCollectReq.setBizType(2);
        return QResult.success(userCaService.caReceive(caCollectReq));
    }

    /**
     * 竞赛电子证书领取
     *
     * @param caCollectReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/plus/ca-receive/contest")
    public QResult caCollectContest(@RequestBody @Validated CompetitionCaCollectRequest caCollectReq,
                                 BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        caCollectReq.setCurrentUser(RequestContext.getCurUser());
        caCollectReq.setQaReceive(Boolean.TRUE);
        caCollectReq.setBizType(2);
        return QResult.success(userCaService.caCollectContest(caCollectReq));
    }

    /**
     * 竞赛电子证书领取-check
     *
     * @param caCollectReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/check/ca-receive/contest")
    public QResult caCheckCollectContest(@RequestBody @Validated CompetitionCaCollectRequest caCollectReq,
                                    BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        caCollectReq.setCurrentUser(RequestContext.getCurUser());
        caCollectReq.setQaReceive(Boolean.TRUE);
        caCollectReq.setBizType(4);
        userCaService.caCollectContest(caCollectReq);
        return QResult.success(null);
    }
    /**
     * 检验竞赛纸质证书证书领取
     *
     * @param caCollectReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/check/receive-paper-contest")
    public QResult checkReceivePaperCollectContest(@RequestBody @Validated CompetitionCaCollectRequest caCollectReq,
                                    BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        caCollectReq.setCurrentUser(RequestContext.getCurUser());
        caCollectReq.setQaReceive(Boolean.TRUE);
        caCollectReq.setBizType(2);
        return QResult.success(userCaService.checkReceivePaperCollectContest(caCollectReq));
    }




    @PostMapping("/mini-app/plus-v2/ca-receive/ca/do")
    public QResult caCollectQQMini(@RequestBody @Validated CompetitionCaCollectRequest caCollectReq,
                              BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        caCollectReq.setCurrentUser(RequestContext.getCurUser());
        caCollectReq.setQaReceive(Boolean.TRUE);
        caCollectReq.setBizType(3);
        return QResult.success(userCaService.caReceive(caCollectReq));
    }

    /**
     * QQ 小程序我的证书
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/user-ca/list")
    public QResult placeOrder(@RequestBody @NotNull UserCaQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(userCaService.list(req));
    }

    @PostMapping("/mini-app/plus/user-ca/list")
    public QResult userCaList(@RequestBody @NotNull UserCaQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        req.setBizType(2);
        return QResult.success(userCaService.list(req));
    }

    /**
     * QQ 小程序获取证书信息
     *
     * @param id
     * @return
     */
    @GetMapping("/mini-app/user-ca/info")
    public QResult getCaInfo(@NotNull @RequestParam("id") String id) {
        return QResult.success(userCaService.getUserCaInfo(id, RequestContext.getUserId()));
    }


    @GetMapping("/mini-app/user-ca/check/receive")
    public QResult checkCaReceive(String activityId){
        userCaService.checkCaReceive(activityId,RequestContext.getUserId());
        return QResult.success(null);
    }

    @GetMapping("/mini-app/user-ca/history")
    public QResult caReceiveHistory(@RequestParam(required = false) String activityId){
        return QResult.success(userCaReceiveHistoryService.list(RequestContext.getCurUser(), RequestContext.getCurUserIdList(),activityId));
    }

    @GetMapping("/mini-app/not/receive/ca")
    public QResult notReceiveCa(){
        String userId = RequestContext.getUserId();
        return QResult.success(caReceiveAdminService.littlePageInfo(userId));
    }

    @GetMapping("/mini-app/receive/ca/ad/popups")
    public QResult caReceiveAdPopups(@NotNull @RequestParam("activityId") String activityId){
        PayConfigPage payConfigPage = activityPayConfigService.findByActivityId(activityId);
        String adPopups = payConfigPage == null? null:payConfigPage.getAdPopups();
        CaAdPopusVo dto = new CaAdPopusVo();
//        dto.setShow(StringUtils.isNotBlank(adPopups));
        dto.setShow(payConfigPage.getShowAdPopups());
        dto.setAdPopups(adPopups);
        return QResult.success(dto);
    }

    /**
     * 初始化纸质证书订单领取
     *
     * @param activityId
     * @return
     */
    @GetMapping("/public/init/paper-ca")
    public QResult initOrderPaperCa(@NotNull @RequestParam("activityId") String activityId) {
        userPaperCaReceiveHistoryService.initPaperOrder(activityId);
        return QResult.success(null);
    }
}
